package com.baidu.bos.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.activiti.engine.ProcessEngine;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baidu.bos.dao.IWorkordermanageDao;
import com.baidu.bos.domain.Workordermanage;
import com.baidu.bos.service.IWorkordermanageService;
import com.baidu.bos.utils.PageBean;
@Service
@Transactional
public class WorkordermanageServiceImpl implements IWorkordermanageService{
	@Resource
	private IWorkordermanageDao workordermanageDao;
	@Resource
	private ProcessEngine processEngine;
	
	public void save(Workordermanage model) {
		workordermanageDao.save(model);
	}

	public void pageQuery(PageBean pageBean) {
		workordermanageDao.pageQuery(pageBean);
	}

	public List<Workordermanage> findListNotStart() {
		DetachedCriteria criteria = DetachedCriteria.forClass(Workordermanage.class);
		criteria.add(Restrictions.eq("start", "0"));
		return workordermanageDao.findByCriteria(criteria);
	}

	public void start(String id) {
		//根据工作单id查询工作单对象
		Workordermanage workordermanage = workordermanageDao.findById(id);
		//将工作单的启动状态设置为1,代表已经启动
		workordermanage.setStart("1");
		//启动流程实例
		String processDefinitionKey = "transfer";
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("业务数据", workordermanage);
		processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey, variables);
	}

	public void update(Workordermanage workordermanage) {
		workordermanageDao.update(workordermanage);
	}
}
